package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianxia.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/*
 * 天下信用报告记录-个人情况&风险
 *
 * @author:szw
 * */

public class TxCreditFengXianParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 天下信用报告记录-个人情况&风险
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/01 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TxCreditReportRecord obj = (TxCreditReportRecord) model;
        TxCreditReportData data = obj.getData();
        // 创建字符串序列化对象
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        SummaryInfo summaryInfo = data.getSummaryInfo();
        if (summaryInfo != null) {
            List<SummaryInfoData> summaryInfoList = summaryInfo.getData() == null ? new ArrayList<>() : summaryInfo.getData();  // 返回数组对象
            if (!summaryInfoList.isEmpty()) {
                for (SummaryInfoData summary : summaryInfoList) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(summary.getContent() == null ? "" : summary.getContent()).append(CRT001);
                    TxSb.append(summary.getStatus() == null ? "" : summary.getStatus()).append(CRT001);
                    TxSb.append("summaryInfo").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        RiskScanInfo riskScanInfo = data.getRiskScanInfo();
        if (riskScanInfo != null) {
            List<RiskScanInfoData> riskScanInfoDataList = riskScanInfo.getData() == null ? new ArrayList<>() : riskScanInfo.getData();   // 返回数组对象
            if (!riskScanInfoDataList.isEmpty()) {
                for (RiskScanInfoData scan : riskScanInfoDataList) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(scan.getName() == null ? "" : scan.getName()).append(CRT001);
                    TxSb.append(scan.getStatus() == null ? "" : scan.getStatus()).append(CRT001);
                    TxSb.append("riskScanInfo").append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            }
        }

        AssetAllocationInfo assetAllocationInfo = data.getAssetAllocationInfo();
        if (assetAllocationInfo != null) {
            if (assetAllocationInfo.getData() != null) {
                List<AssetAllocationInfoItem> assetAllocationInfoItemList = assetAllocationInfo.getData().getItems() == null ? new ArrayList<>() : assetAllocationInfo.getData().getItems();   // 返回数组对象
                if (!assetAllocationInfoItemList.isEmpty()) {
                    for (AssetAllocationInfoItem aa : assetAllocationInfoItemList) {
                        TxSb.delete(0, TxSb.length());
                        TxSb.append(aa.getKey() == null ? "" : aa.getKey()).append(CRT001);
                        TxSb.append(aa.getValue() == null ? "" : aa.getValue()).append(CRT001);
                        TxSb.append("assetAllocationInfo").append(CRT001);
                        key.set(getKey());
                        val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                        context.write(key, val);
                    }
                }
            }
        }

        return summaryInfo != null || riskScanInfo != null || assetAllocationInfo != null;
    }

    @Override
    public String getSubType() {
        return ".fengxian";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tx_credit_fengxian_inc";
    }

    @Override
    public String getType() {
        return RecordType.TX_CREDIT_REPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天下信用-个人情况&风险";
    }
}
